#include "st_funs.h"

USER_DATA* login_Check(USER_DATA *tag_data,SESSION_LIST *tag_session)
{
	char tag_string[NAME_SIZE_];
	SESSION_NODE *tag_node;
	char *cookie;
	
	/*读取user_name*/
	cookie=getenv("HTTP_COOKIE");
	if(cookie==NULL)return NULL;
	ca_Get(tag_string,"user",cookie);
		
	tag_node=session_Get(tag_string,tag_session);
	
	if(tag_node==NULL)
	{
		return NULL;
	}
	else
	{	
		ca_Get(tag_string,"id",cookie);

		if(atoi(tag_string)==tag_node->session_id)
		{
			memcpy(tag_data,&tag_node->user_data,sizeof(USER_DATA));
			return tag_data;
		}
		else
		{
			return NULL;
		};
	};
	
	return NULL;
};

USER_DATA* user_Check(USER_DATA *tag_data,DB_LINKER *tag_db)
{
	char user_id[NAME_SIZE_];
	char user_pas[NAME_SIZE_];
	char *user_name;
	char *user_no;
	char post_data[200];
	char sql_command[SQL_LENGTH_];
	DB_DATA res_data;
	DB_ROW res_row;
	
	if(get_Post(post_data)==NULL)return NULL;
	ca_Get(user_id,"user",post_data);
	ca_Get(user_pas,"password",post_data);
	
	if(!input_Check(user_id)||!input_Check(user_pas))return NULL;
	
	sprintf(sql_command,SQL_CHECK_USER_,user_id,user_pas);
	if(db_Query(&res_data,sql_command,tag_db)==NULL)return NULL;
	if(db_Fetch(&res_row,&res_data)==NULL)return NULL;
	
	user_no=db_Get(0,&res_row);
	user_name=db_Get(1,&res_row);
	
	strcpy(tag_data->user_name,user_name);
	strcpy(tag_data->user_id,user_no);
	
	tag_data->user_level=ST;
	
	db_Free_data(&res_data);
			
	return tag_data;
};

BOOL_ input_Check(char *in_char)
{
	return TRUE_;
};

char* level_Convert(C_ARRAY *tag_string,int level)
{
	switch(level)
	{
		case 1:
		{
			*tag_string=string_Set("高一上学期",*tag_string);
			break;
		}
		case 2:
		{
			*tag_string=string_Set("高一下学期",*tag_string);
			break;
		}
		case 3:
		{
			*tag_string=string_Set("高二上学期",*tag_string);
			break;
		}
		case 4:
		{
			*tag_string=string_Set("高二下学期",*tag_string);
			break;
		}
		case 5:
		{
			*tag_string=string_Set("高三上学期",*tag_string);
			break;
		}
		case 6:
		{
			*tag_string=string_Set("高三下学期",*tag_string);
			break;
		}
		default:
		{
			*tag_string=string_Set("未知",*tag_string);
			break;
		};
	};
	return (char*)*tag_string;
};


void show_Page(char *in_html,char *str1,char *str2,char *str3)
{
	char *op;
	
	printf("Content-Type:text/html;charset=utf-8\n\n");

	for(op=in_html;*op!='\0';++op)
	{
		if(*op=='%')
		{
			if(*(op+1)=='1')
			{
				printf(str1);
				op+=2;
			}
			else if(*(op+1)=='2')
			{
				printf(str2);
				op+=2;
			}
			else if(*(op+1)=='3')
			{
				printf(str3);
				op+=2;
			}
			else
			{
				putc(*op,stdout);
			};
			
		}
		else
		{
			putc(*op,stdout);
		};
	};
};

void show_Login(char *in_html)
{
	char str1[]="游客<p align=right><a href=\"/st/\">登录</a>";
	char str2[]="<p>输入用户名和密码然后点登录";
	char str3[]="<p>请登录<form method=\"post\" action=\"/st/?op=login\"><p><input type=\"text\" name=\"user\" /><p><input type=\"text\" name=\"password\" /><p><input type=\"submit\" /></form>";
	
	show_Page(in_html,str1,str2,str3);
};

void show_Main(char *in_html,USER_DATA *user_info)
{
	
	char str1[NAME_SIZE_];
	char str2[]=SIDEBAR_LOGINED_TEXT_;
	char str3[]="<p>欢迎登录";
	
	sprintf(str1,"%s%s",user_info->user_name,LOGOUT_TEXT_);
		
	show_Page(in_html,str1,str2,str3);
	
};

void show_Info(char *in_html,USER_DATA *user_info,DB_LINKER *tag_db)
{
	char *op;
	char str1[NAME_SIZE_];
	char str2[]=SIDEBAR_LOGINED_TEXT_;
	
	char sql_command[SQL_LENGTH_];
	DB_DATA res_data;
	DB_ROW res_row;
	
	sprintf(str1,"%s%s",user_info->user_name,LOGOUT_TEXT_);
	
	sprintf(sql_command,SQL_CHECK_INFO_,user_info->user_id);
	if(db_Query(&res_data,sql_command,tag_db)==NULL)
	{
		show_Error();
		return;
	};
	
	debug_Output(sql_command);
		
	printf("Content-Type:text/html;charset=utf-8\n\n");

	for(op=in_html;*op!='\0';++op)
	{
		if(*op=='%')
		{
			if(*(op+1)=='1')
			{
				printf(str1);
				op+=2;
			}
			else if(*(op+1)=='2')
			{
				printf(str2);
				op+=2;
			}
			else if(*(op+1)=='3')
			{
				printf(INFO_TABLE_HEAD_);
				
				while(db_Fetch(&res_row,&res_data)!=NULL)
				{
					if(!strcmp("0",db_Get(0,&res_row)))printf(INFO_TABLE_LINE_,"姓名",db_Get(1,&res_row));
				};
				printf(TABLE_TAIL_);
				
				db_Free_data(&res_data);
				op+=2;
			}
			else
			{
				putc(*op,stdout);
			};
			
		}
		else
		{
			putc(*op,stdout);
		};
	};
	
};

void show_Score(char *in_html,USER_DATA *user_info,HTML_OP *get_op,DB_LINKER *tag_db)
{
	char *op;
	char str1[NAME_SIZE_];
	char str2[]=SIDEBAR_SCORE_TEXT_;
	
	char sql_command[SQL_LENGTH_];
	DB_DATA res_data;
	DB_ROW res_row;
	C_ARRAY grade_level;
	
	sprintf(str1,"%s%s",user_info->user_name,LOGOUT_TEXT_);
	
	printf("Content-Type:text/html;charset=utf-8\n\n");

	if(!strcmp(get_op->var1,"all"))
	{
		sprintf(sql_command,SQL_CHECK_SCORE_ALL_,user_info->user_id,user_info->user_id);
		if(db_Query(&res_data,sql_command,tag_db)==NULL)
		{
			show_Error();
			return;
		};
	
		debug_Output(sql_command);
	
		grade_level=array_Create(sizeof(char));
		
		for(op=in_html;*op!='\0';++op)
		{
			if(*op=='%')
			{
				if(*(op+1)=='1')
				{
					printf(str1);
					op+=2;
				}
				else if(*(op+1)=='2')
				{
					printf(str2);
					op+=2;
				}
				else if(*(op+1)=='3')
				{
					printf(SCORE_TABLE_HEAD_);
				
					while(db_Fetch(&res_row,&res_data)!=NULL)
					{
						printf(SCORE_TABLE_LINE_,db_Get(0,&res_row),level_Convert(&grade_level,atoi(db_Get(1,&res_row))),db_Get(2,&res_row),db_Get(3,&res_row),db_Get(4,&res_row));
					};
				
					printf(TABLE_TAIL_);
					db_Free_data(&res_data);
					op+=2;
				}
				else
				{
					putc(*op,stdout);
				};
			
			}
			else
			{
				putc(*op,stdout);
			};
		};
	}
	else if(!strcmp(get_op->var1,"nopass"))
	{
		sprintf(sql_command,SQL_CHECK_SCORE_NOPASS_,user_info->user_id,user_info->user_id);
		if(db_Query(&res_data,sql_command,tag_db)==NULL)
		{
			show_Error();
			return;
		};
	
		debug_Output(sql_command);
	
		grade_level=array_Create(sizeof(char));
		
		for(op=in_html;*op!='\0';++op)
		{
			if(*op=='%')
			{
				if(*(op+1)=='1')
				{
					printf(str1);
					op+=2;
				}
				else if(*(op+1)=='2')
				{
					printf(str2);
					op+=2;
				}
				else if(*(op+1)=='3')
				{
					printf(SCORE_TABLE_HEAD_);
				
					while(db_Fetch(&res_row,&res_data)!=NULL)
					{
						printf(SCORE_TABLE_LINE_,db_Get(0,&res_row),level_Convert(&grade_level,atoi(db_Get(1,&res_row))),db_Get(2,&res_row),db_Get(3,&res_row),db_Get(4,&res_row));
					};
				
					printf(TABLE_TAIL_);
					db_Free_data(&res_data);
					op+=2;
				}
				else
				{
					putc(*op,stdout);
				};
			
			}
			else
			{
				putc(*op,stdout);
			};
		};
	}
	else
	{
		sprintf(sql_command,SQL_CHECK_SCORE_ALL_,user_info->user_id,user_info->user_id);
		if(db_Query(&res_data,sql_command,tag_db)==NULL)
		{
			show_Error();
			return;
		};
	
		debug_Output(sql_command);
	
		grade_level=array_Create(sizeof(char));
		
		for(op=in_html;*op!='\0';++op)
		{
			if(*op=='%')
			{
				if(*(op+1)=='1')
				{
					printf(str1);
					op+=2;
				}
				else if(*(op+1)=='2')
				{
					printf(str2);
					op+=2;
				}
				else if(*(op+1)=='3')
				{
					printf(SCORE_TABLE_HEAD_);
				
					while(db_Fetch(&res_row,&res_data)!=NULL)
					{
						printf(SCORE_TABLE_LINE_,db_Get(0,&res_row),level_Convert(&grade_level,atoi(db_Get(1,&res_row))),db_Get(2,&res_row),db_Get(3,&res_row),db_Get(4,&res_row));
					};
				
					printf(TABLE_TAIL_);
					db_Free_data(&res_data);
					op+=2;
				}
				else
				{
					putc(*op,stdout);
				};
			
			}
			else
			{
				putc(*op,stdout);
			};
		};
	};
	
	array_Drop(grade_level);
};

void show_Error()
{
	printf("Content-Type:text/html;charset=utf-8\n\n");
	printf("Server error");
};


HTML_OP* op_Get(HTML_OP *tag_op,char *get_data)
{
	char op[NAME_SIZE_];
		
	if(ca_Get(op,"op",get_data)==NULL)
	{
		tag_op->type=NONE;
		return tag_op;
	};
	
	ca_Get(tag_op->var1,"var1",get_data);
		
	if(!strcmp(op,"login"))
	{
		tag_op->type=LOGIN;
	}
	else if(!strcmp(op,"logout"))
	{
		tag_op->type=LOGOUT;
	}
	else if(!strcmp(op,"info"))
	{
		tag_op->type=INFO;
	}
	else if(!strcmp(op,"news"))
	{
		tag_op->type=NEWS;
	}
	else if(!strcmp(op,"score"))
	{
		tag_op->type=SCORE;
	}
	else
	{
		tag_op->type=NONE;
	};
	return tag_op;
};
